In [2]:
from PIL import Image
import os, sys

path = "images/"
dirs = os.listdir( path )

def resize():
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item)
            f, e = os.path.splitext(path+item)
            imResize = im.resize((100,100), Image.ANTIALIAS)
            imResize.save(f + ' resized.jpg', 'JPEG', quality=90)

resize()
In [33]:
from PIL import Image
#file=str(k)+'.jpg'
im = Image.open("4.jpg")
croppedIm = im.crop((0,0, 1300, 1300))
croppedIm.save('cropped_4.png')
In [ ]:
 
In [34]:
from PIL import Image
text_file = open("data_cluster.csv", "w")
for k in range (11,12):
 #file=str(k)+'.png'
 file="cropped_1"+'.png'
 im = Image.open(file)

 im1 = im.convert('YCbCr')
 im3 = im.convert('CMYK')

 width, height = im.size

 im2 = Image.new('RGBA', (width, height))

 for i in range(0,width):
  for j in range(0,height):
   pixel=im.getpixel((i, j))
   pixel1=im1.getpixel((i, j))
   pixel2=im3.getpixel((i, j))
  #print pixel[0]
   #print pixel1
   text_file.write('%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n'%(pixel1[0],pixel1[1],pixel1[2],pixel2[0],pixel2[1],pixel2[2],pixel[0],pixel[1],pixel[2],1))
   
  
text_file.close()
In [35]:
import numpy as np
import pandas as pd
from time import time
from IPython.display import display # Allows the use of display() for DataFrames


import visuals as vs


%matplotlib inline


data = pd.read_csv("data_cluster.csv")

# Success - Display the first record
display(data.head(n=10))
101 125 109 179 140 158 76 115 97 1
0 86 125 109 194 155 173 61 100 82 1
1 79 125 108 203 161 181 52 94 74 1
2 95 125 108 187 145 165 68 110 90 1
3 99 125 108 183 141 161 72 114 94 1
4 92 125 108 190 148 168 65 107 87 1
5 97 125 110 183 144 163 72 111 92 1
6 100 125 110 178 141 159 77 114 96 1
7 101 125 112 175 141 158 80 114 97 1
8 91 125 112 185 152 169 70 103 86 1
9 89 125 113 185 154 170 70 101 85 1
In [36]:
X= data.drop('1', axis = 1)
In [37]:
from sklearn.decomposition import PCA
n_components = 2
pca = PCA(n_components=n_components, whiten=True).fit(X)
In [15]:
pca_results = vs.pca_results(X, pca)
In [38]:
reduced_data = pca.transform(X)
reduced_data = pd.DataFrame(reduced_data, columns = ['Dimension 1', 'Dimension 2'])
In [17]:
vs.biplot(X, reduced_data, pca)
Out[17]:
<matplotlib.axes.AxesSubplot at 0x7f68e509ea90>
/usr/lib/pymodules/python2.7/matplotlib/collections.py:548: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == 'face':
In [39]:
clusterer = GMM(n_components=3,random_state=0).fit(reduced_data)


preds = clusterer.predict(reduced_data)


centers = clusterer.means_
In [22]:
indices = [10,50,80]


samples = pd.DataFrame(data.loc[indices], columns = data.keys()).reset_index(drop = True)
print "Chosen samples of wholesale customers dataset:"
samples=samples.drop('1', axis = 1)

display(samples)
Chosen samples of wholesale customers dataset:
101 125 109 179 140 158 76 115 97
0 93 125 115 179 152 167 76 103 88
1 98 130 107 186 143 152 69 112 103
2 99 125 112 177 144 161 78 111 94
In [23]:
pca_samples = pca.transform(samples)
In [24]:
vs.cluster_results(reduced_data, preds, centers,pca_samples)
In [ ]:
 
In [40]:
import warnings
warnings.filterwarnings("ignore")
from PIL import Image
count_plant=0
count_non_plant=0
count_dead_plant=0
count=0
im = Image.open('4.jpg')

im1 = im.convert('YCbCr')
im3 = im.convert('CMYK')

width, height = im.size

im2 = Image.new('RGBA', (width, height))
imn = Image.new('RGBA', (width, height))
imm = Image.new('RGBA', (width, height))

for i in range(0,width):
 for j in range(0,height):
   pixel=im.getpixel((i, j))
   pixel1=im1.getpixel((i, j))
   pixel2=im3.getpixel((i, j))
  #print pixel[0]
   #print pixel1
   X_image=[pixel1[0],pixel1[1],pixel1[2],pixel2[0],pixel2[1],pixel2[2],pixel[0],pixel[1],pixel[2]] 
   
   count=count+1
   #text_file.write('%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n'%(pixel1[0],pixel1[1],pixel1[2],pixel2[0],pixel2[1],pixel2[2],pixel[0],pixel[1],pixel[2],1))
   
  #if ((pixel1[0]<=255 and pixel1[0]>=210) and (pixel1[1]<=128 and pixel1[1]>=105) ):
   if result==1:
    im2.putpixel((i, j), (pixel[0], pixel[1], pixel[2]))
    imm.putpixel((i, j), (255, 255, 255))
    imn.putpixel((i, j), (255, 255, 255))
    count_plant=count_plant+1
    #im2.putpixel((i, j), (255, 255, 255))
   elif result==2:
    im2.putpixel((i, j), (255, 255, 255))
    imm.putpixel((i, j), (255, 255, 255))
    imn.putpixel((i, j), (pixel[0], pixel[1], pixel[2]))
    count_dead_plant=count_dead_plant+1
    #count_plant=count_plant+1
   elif result==0:
    im2.putpixel((i, j), (255, 255, 255))
    imn.putpixel((i, j), (255, 255, 255))
    imm.putpixel((i, j), (pixel[0], pixel[1], pixel[2]))
    count_non_plant=count_non_plant=0+1
    #im2.putpixel((i, j), (pixel[0], pixel[1], pixel[2])) 
     
im2.save('newwin_cropped_4.png') 
imn.save('newwin_cropped1_4.png') 
imm.save('newwin_cropped1_4_4.png') 
    
    #im2.putpixel((i, j), (pixel[0], pixel[1], pixel[2]))
plant_percent=(count_plant*1.0/count*1.0)*100  
Dead_plant_percent=(count_dead_plant*1.0/count*1.0)*100 
LAI=(plant_percent*1.0/(100-plant_percent)*1.0)
In [24]:
%matplotlib inline
from PIL import Image
import matplotlib.pyplot as plt
def displayimage(image):
    fig=plt.figure()
    plt.imshow(image)
    plt.show()

Results

In [27]:
im = Image.open('1.jpg')
im2 = Image.open('newwin_cropped_1.png')
im3 = Image.open('newwin_cropped1_1.png')
im4 = Image.open('newwin_cropped1_1_1.png')
displayimage(im)
displayimage(im2)
displayimage(im3)
displayimage(im4)
print ("Plant percentage is {}".format(Dead_plant_percent))
print ("Dead Plant percentage is {}".format(plant_percent))
print ("Leaf Area Index is {}".format(LAI))
Plant percentage is 23.3450390363
Dead Plant percentage is 40.2950415437
Leaf Area Index is 0.674902764956
In [4]:
LAI=23.3/(100-23.3)
print ("Leaf Area Index of above is {}".format(LAI))
Leaf Area Index of above is 0.303780964798
In [14]:
im = Image.open('7.jpg')
im2 = Image.open('newwin_cropped_7.png')
im3 = Image.open('newwin_cropped1_7.png')
im4 = Image.open('newwin_cropped1_7_7.png')
displayimage(im)
displayimage(im2)
displayimage(im3)
displayimage(im4)
print ("Plant percentage is {}".format(plant_percent))
print ("Dead Plant percentage is {}".format(Dead_plant_percent))
print ("Leaf Area Index is {}".format(LAI))
Plant percentage is 36.1902991122
Dead Plant percentage is 42.3603132261
Leaf Area Index is 0.567159830068
In [25]:
im = Image.open('11.jpg')
im2 = Image.open('newwin_cropped_11.png')
im3 = Image.open('newwin_cropped1_11.png')
im4 = Image.open('newwin_cropped1_11_11.png')
displayimage(im)
displayimage(im2)
displayimage(im3)
displayimage(im4)
print ("Plant percentage is {}".format(plant_percent))
print ("Dead Plant percentage is {}".format(Dead_plant_percent))
print ("Leaf Area Index is {}".format(LAI))
Plant percentage is 38.5985279669
Dead Plant percentage is 32.8947802142
Leaf Area Index is 0.628625449664
In [30]:
im = Image.open('13.jpg')
im2 = Image.open('newwin_cropped_13.png')
im3 = Image.open('newwin_cropped1_13.png')
im4 = Image.open('newwin_cropped1_13_13.png')
displayimage(im)
displayimage(im2)
displayimage(im3)
displayimage(im4)
print ("Plant percentage is {}".format(plant_percent))
print ("Dead Plant percentage is {}".format(Dead_plant_percent))
print ("Leaf Area Index is {}".format(LAI))
Plant percentage is 44.9057612508
Dead Plant percentage is 40.1012730752
Leaf Area Index is 0.815071816407
In [ ]: